﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CardsServer.DAL;
using System.Transactions;
using System.Linq.Expressions;
using LinqToSqlExtensions;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string ProductIN_Add(ProductIN proin, int ACID);
        [OperationContract]
        string ProductIN_Update(ProductIN proin, int ACID);
        [OperationContract]
        ProductIN ProductIN_ByBillno_Get(string billno, string sortBy, int ACID);

        #region 产品入库报表
        /// <summary>
        /// 入库订单明细表
        /// </summary>
        /// <param name="begtime"></param>
        /// <param name="endtime"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<Get_ProductIN_BillnoDetailResult> Get_ProductIN_BillnoDetail(DateTime? begtime,DateTime? endtime,int ACID);
        /// <summary>
        /// 入库订单汇总
        /// </summary>
        /// <param name="begtime"></param>
        /// <param name="endtime"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<Get_ProductIN_BillnoCollectResult> Get_ProductIN_BillnoCollect(DateTime? begtime, DateTime? endtime, int ACID);
        /// <summary>
        /// 入库产品明细
        /// </summary>
        /// <param name="begtime"></param>
        /// <param name="endtime"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<Get_ProductIN_GoodCollectResult> Get_ProductIN_GoodCollect(DateTime? begtime, DateTime? endtime, int ACID);
        /// <summary>
        /// 供应商入库汇总
        /// </summary>
        /// <param name="begtime"></param>
        /// <param name="endtime"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<Get_ProductIN_SupplierCollectResult> Get_ProductIN_SupplierCollect(DateTime? begtime, DateTime? endtime, int ACID);
        /// <summary>
        /// 供应商订单汇总
        /// </summary>
        /// <param name="begtime"></param>
        /// <param name="endtime"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<Get_ProductIN_SupBilCollectResult> Get_ProductIN_SupBilCollect(DateTime? begtime, DateTime? endtime, int ACID);
        #endregion
    }

    public partial class Service : IService
    {

        #region
        /// <summary>
        /// 产品入库
        /// </summary>
        /// <param name="proin"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string ProductIN_Add(ProductIN proin, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        string r1 = ProductIN_M_Add(proin.M, ACID, ctx);
                        if (!string.IsNullOrEmpty(r1))
                        {
                            throw new Exception(r1);
                        }
                        foreach (ProductIN_D item in proin.D)
                        {
                            string r2 = ProductIN_D_Add(item, ACID, ctx);
                            if (!string.IsNullOrEmpty(r2))
                            {
                                throw new Exception(r2);
                            }
                            onhand h = GetonhandByMoreKey(item.gcode, item.storage, ACID);
                            if (h != null)
                            {
                                h.onhandcount += proin.M.billnoTypes == 1 ? decimal.Parse(item.counts.ToString()) :
                                    (-1) * decimal.Parse(item.counts.ToString());
                            }
                            else {
                                if (proin.M.billnoTypes == 1)
                                {
                                    h = new onhand();
                                    h.GoodsCode = item.gcode;
                                    h.WarehouseID = item.storage;
                                    h.onhandcount = decimal.Parse(item.counts.ToString());
                                    onhand_Add(h, ACID);
                                }
                            }
                        }
                        
                        RecordBalance(proin.M.total, proin.M.clientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }
        /// <summary>
        /// 修改入库单信息
        /// </summary>
        /// <param name="proin"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string ProductIN_Update(ProductIN proin, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        string r1 = ProductIN_M_Update (proin.M, ACID, ctx);
                        if (!string.IsNullOrEmpty(r1))
                        {
                            throw new Exception(r1);
                        }
                        foreach (ProductIN_D item in proin.D)
                        {
                            string r2 = ProductIN_D_Update(item, ACID, ctx);
                            if (!string.IsNullOrEmpty(r2))
                            {
                                throw new Exception(r2);
                            }
                        }
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }
        /// <summary>
        /// 获取入库单信息
        /// </summary>
        /// <param name="billno"></param>
        /// <param name="sortBy"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public ProductIN ProductIN_ByBillno_Get(string billno, string sortBy, int ACID)
        {
            Expression<Func<ProductIN_D, bool>> predicate_d = PredicateBuilder.True<ProductIN_D>();
            predicate_d=p => p.billid == billno;

            ProductIN pro = new ProductIN();
            pro.M =ProductIN_MBydid_Get(billno, ACID);
            pro.D = ProductIN_D_Get(predicate_d, sortBy, ACID);

            return pro;
        }
        #endregion

        #region 报表
        public List<Get_ProductIN_BillnoDetailResult> Get_ProductIN_BillnoDetail(DateTime? begtime, DateTime? endtime, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (begtime == null || endtime == null)
                {
                    begtime = DateTime.Parse("1900-01-01 00:00:00");
                    endtime = DateTime.Parse("2999-12-30 23:59:59");
                }
                return ctx.Get_ProductIN_BillnoDetail(begtime, endtime, "").ToList();
            }
        }

        public List<Get_ProductIN_BillnoCollectResult> Get_ProductIN_BillnoCollect(DateTime? begtime, DateTime? endtime, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (begtime == null || endtime == null)
                {
                    begtime = DateTime.Parse("1900-01-01 00:00:00");
                    endtime = DateTime.Parse("2999-12-30 23:59:59");
                }
                return ctx.Get_ProductIN_BillnoCollect(begtime, endtime).ToList();
            }
        }

        public List<Get_ProductIN_GoodCollectResult> Get_ProductIN_GoodCollect(DateTime? begtime, DateTime? endtime, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (begtime == null || endtime == null)
                {
                    begtime = DateTime.Parse("1900-01-01 00:00:00");
                    endtime = DateTime.Parse("2999-12-30 23:59:59");
                }
                return ctx.Get_ProductIN_GoodCollect(begtime, endtime, "", "").ToList();
            }
        }

        public List<Get_ProductIN_SupplierCollectResult> Get_ProductIN_SupplierCollect(DateTime? begtime, DateTime? endtime, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (begtime == null || endtime == null)
                {
                    begtime = DateTime.Parse("1900-01-01 00:00:00");
                    endtime = DateTime.Parse("2999-12-30 23:59:59");
                }
                return ctx.Get_ProductIN_SupplierCollect(begtime, endtime, "").ToList();
            }
        }

        public List<Get_ProductIN_SupBilCollectResult> Get_ProductIN_SupBilCollect(DateTime? begtime, DateTime? endtime, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (begtime == null || endtime == null)
                {
                    begtime = DateTime.Parse("1900-01-01 00:00:00");
                    endtime = DateTime.Parse("2999-12-30 23:59:59");
                }
                return ctx.Get_ProductIN_SupBilCollect(begtime, endtime, "").ToList();
            }
        }
        #endregion
    }

    /// <summary>
    /// 产品入库单
    /// </summary>
    public class ProductIN 
    {
        public ProductIN_M M { get; set; }
        public List<ProductIN_D> D { get; set; }
    }
}
